﻿Public Class clsInhTrPozycjaZlecenia
    Inherits clsTrPozycjeZlecenia

#Region "Attributes"

    Private mvarGatunekMaterialu As clsGatunek
    Private mvarJednostka As clsJednostkaMiary
    Private mvarPakowanie As clsPakowanie
    Private mvarMagazyn As clsMagazyn
    Private mvarStatus As clsStatus

    Private mvarSchematyProdukcji As Collection(Of clsInhTrSchemat)
    Private mvarSchematyProdukcjiDel As Collection(Of clsInhTrSchemat)

    Private mvarPozycjeZlecenia As New DataTable()

#End Region

#Region "Properties"

    Public Property GatunekMaterialu As clsGatunek
        Get
            Return mvarGatunekMaterialu
        End Get
        Set(ByVal value As clsGatunek)
            mvarGatunekMaterialu = value
        End Set
    End Property

    Public Property Jednostka As clsJednostkaMiary
        Get
            Return mvarJednostka
        End Get
        Set(ByVal value As clsJednostkaMiary)
            mvarJednostka = value
        End Set
    End Property

    Public Property Pakowanie As clsPakowanie
        Get
            Return mvarPakowanie
        End Get
        Set(ByVal value As clsPakowanie)
            mvarPakowanie = value
        End Set
    End Property

    Public Property Status As clsStatus
        Get
            Return mvarStatus
        End Get
        Set(ByVal value As clsStatus)
            mvarStatus = value
        End Set
    End Property

    Public Property SchematyProdukcji() As Collection(Of clsInhTrSchemat)
        Get
            Return mvarSchematyProdukcji
        End Get
        Set(ByVal value As Collection(Of clsInhTrSchemat))
            mvarSchematyProdukcji = value
        End Set
    End Property

    Public Property SchematyProdukcjiDel() As Collection(Of clsInhTrSchemat)
        Get
            Return mvarSchematyProdukcjiDel
        End Get
        Set(ByVal value As Collection(Of clsInhTrSchemat))
            mvarSchematyProdukcjiDel = value
        End Set
    End Property

    Public Property Magazyn As clsMagazyn
        Get
            Return mvarMagazyn
        End Get
        Set(ByVal value As clsMagazyn)
            mvarMagazyn = value
        End Set
    End Property

    Public Property PozycjeZlecenia() As DataTable
        Get
            Return mvarPozycjeZlecenia
        End Get
        Set(ByVal value As DataTable)
            mvarPozycjeZlecenia = value
        End Set
    End Property

#End Region

#Region "Methods"

    Public Sub New()
        MyBase.New()
        mvarSchematyProdukcji = New Collection(Of clsInhTrSchemat)
        mvarSchematyProdukcjiDel = New Collection(Of clsInhTrSchemat)
    End Sub

    Public Sub New(ByVal ID As Integer, ByVal lvarDbHolder As clsDataBase, Wyzn As Boolean) 'wyznacznik informuje czy nalezy rowniez pobrac dane na temat schematow 

        MyBase.New(ID, lvarDbHolder, VAR_ERROR_HANDLER)

        mvarJednostka = New clsJednostkaMiary(MyBase.JednostkaMiaryId, lvarDbHolder, VAR_ERROR_HANDLER)
        mvarGatunekMaterialu = New clsGatunek(MyBase.GatunekId, lvarDbHolder, VAR_ERROR_HANDLER)
        mvarPakowanie = New clsPakowanie(MyBase.PakowaniaId.ToString(), lvarDbHolder, VAR_ERROR_HANDLER)
        mvarMagazyn = New clsMagazyn(MyBase.MagazynId, lvarDbHolder, VAR_ERROR_HANDLER)
        mvarStatus = New clsStatus(MyBase.StatusId, lvarDbHolder, VAR_ERROR_HANDLER)
        'SCHEMATY

        If Wyzn Then
            mvarSchematyProdukcji = New Collection(Of clsInhTrSchemat)
            mvarSchematyProdukcjiDel = New Collection(Of clsInhTrSchemat)

            Dim lvarTabel2 As DataTable = PobierzWszystkieSchematy(lvarDbHolder, MyBase.Id)

            If Not lvarTabel2 Is Nothing Then
                For Each lvarRow As DataRow In lvarTabel2.Rows
                    mvarSchematyProdukcji.Add(New clsInhTrSchemat(CInt(lvarRow.Item("id")), lvarDbHolder))
                Next
            End If
        End If


    End Sub

    Public Overloads Sub Save(ByVal lvarDbHolder As clsDataBase)


        MyBase.Save(lvarDbHolder, VAR_ERROR_HANDLER)

        For Each lvarSchemat As clsInhTrSchemat In mvarSchematyProdukcji
            lvarSchemat.PozycjeZlecenieId = MyBase.Id
            lvarSchemat.Ilosc = Replace(lvarSchemat.Ilosc, ",", ".")
            lvarSchemat.Save(lvarDbHolder)
        Next

        For Each lvarSchemat As clsInhTrSchemat In mvarSchematyProdukcjiDel

            For Each lvarPozycja As clsInhTrPozycjeSchematu In lvarSchemat.PozycjeSchematu
                lvarPozycja.Delete(lvarDbHolder)
            Next

            lvarSchemat.Delete(lvarDbHolder)
        Next

    End Sub

    Public Overloads Sub Delete(ByVal lvarDbHolder As clsDataBase)

        For Each lvarSchemat As clsInhTrSchemat In mvarSchematyProdukcji

            For Each lvarPozycja As clsInhTrPozycjeSchematu In lvarSchemat.PozycjeSchematu
                lvarPozycja.Delete(lvarDbHolder)
            Next

            lvarSchemat.Delete(lvarDbHolder, VAR_ERROR_HANDLER)
        Next

        MyBase.Delete(lvarDbHolder, VAR_ERROR_HANDLER)

    End Sub

    Shared Function PobierzWszystkieSchematy(ByVal lvarDbHolder As clsDataBase, Id As Integer) As DataTable

        Dim lvarReader As OdbcDataReader = Nothing
        Dim lvarReturn As DataTable

        If lvarDbHolder.IsOpen Then
            Dim lvarSqlRunner As New clsSqlRunner(lvarDbHolder, "queries_application_tracking", "cls_pozycje_zlecenia", 1, VAR_ERROR_HANDLER)

            With lvarSqlRunner
                .AddParameter("p_id_pozycje_zlecenia", Id.ToString())
                lvarReader = .ExecuteQuery
            End With

            If lvarReader.HasRows Then
                lvarReturn = New DataTable
                lvarReturn.Load(lvarReader)
            End If

        End If

        Return lvarReturn

    End Function

    Public Sub Szukaj(ByVal lvarDbHolder As clsDataBase, ByVal Zapytanie As String)

        Dim lvarNewRow As DataRow
        Dim lvarZapytanie As String

        lvarZapytanie = Zapytanie

        Dim lvarReader As OdbcDataReader = Nothing
        If lvarDbHolder.IsOpen Then
            Dim lvarSqlRunner As New clsSqlRunner(lvarDbHolder, "queries_application_tracking", "cls_pozycje_zlecenia", 2, VAR_ERROR_HANDLER)

             With lvarSqlRunner
                .AddParameter("p_zapytanie", Zapytanie)
                lvarReader = .ExecuteQuery
            End With

            If lvarReader.HasRows Then
                While lvarReader.Read
                    lvarNewRow = mvarPozycjeZlecenia.NewRow()
                    With lvarNewRow
                        .Item("oem_id") = IIf(IsDBNull(lvarReader.Item("oem_id")), String.Empty, lvarReader.Item("oem_id"))
                        .Item("klient_nazwa") = IIf(IsDBNull(lvarReader.Item("nazwa_skrocona")), String.Empty, lvarReader.Item("nazwa_skrocona"))
                        .Item("termin") = IIf(IsDBNull(lvarReader.Item("termin")), String.Empty, lvarReader.Item("termin"))
                        .Item("zlecenie_id") = IIf(IsDBNull(lvarReader.Item("zlecenie_id")), String.Empty, lvarReader.Item("zlecenie_id"))
                        .Item("pozycja_zlecenia_id") = IIf(IsDBNull(lvarReader.Item("pozycja_zlecenia_id")), String.Empty, lvarReader.Item("pozycja_zlecenia_id"))
                    End With
                    mvarPozycjeZlecenia.Rows.Add(lvarNewRow)
                End While
            End If

        End If

    End Sub

    Shared Function ZmienStatus(ByVal lvarDbHolder As clsDataBase, IdPozycjiZlecenia As Integer, IdStatus As Integer) As Boolean

        Dim lvarReader As OdbcDataReader = Nothing
        Dim lvarReturn As Boolean

        If lvarDbHolder.IsOpen Then
            Dim lvarSqlRunner As New clsSqlRunner(lvarDbHolder, "queries_application_tracking", "cls_pozycje_zlecenia", 3, VAR_ERROR_HANDLER)
            '      

            With lvarSqlRunner
                .AddParameter("p_pozycja_zlecenia_id", IdPozycjiZlecenia.ToString())
                .AddParameter("p_status_id", IdStatus.ToString())
                lvarReader = .ExecuteQuery
            End With

        End If

        Return lvarReturn

    End Function

#End Region

End Class
